package com.turnengine.client.global.game.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.global.game.bean.GameVersionSerializer;
import com.turnengine.client.global.game.bean.IGameVersion;
import java.io.IOException;

/**
 * The Get Game Version By Name Data Serializer.
 */
public class GetGameVersionByNameDataSerializer extends AbstractCommandDataSerializer<IGameVersion> implements IGetGameVersionByNameDataSerializer {

	/**
	 * Creates a new Get Game Version By Name Data Serializer.
	 */
	public GetGameVersionByNameDataSerializer() {
		super(GetGameVersionByName.COMMAND_ID);
	}

	@Override
	public IGameVersion readReturnValue(IDataReader reader) throws IOException {
		IGameVersion value = new GameVersionSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IGameVersion value) throws IOException {
		new GameVersionSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IGameVersion> readRequest(IDataReader reader) throws IOException {

		// Read fields
		String versionName = reader.readString(true);
		int gameId = reader.readInt();

		// Create object
		GetGameVersionByName object = new GetGameVersionByName();
		object.setVersionName(versionName);
		object.setGameId(gameId);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IGameVersion> argument) throws IOException {
		GetGameVersionByName object = (GetGameVersionByName) argument;

		// Get fields
		String versionName = object.getVersionName();
		int gameId = object.getGameId();

		// Write fields
		writer.writeString(versionName, true);
		writer.writeInt(gameId);
	}

	@Override
	public IExecutableCommandResponse<IGameVersion> newResponse() {
		return new GetGameVersionByNameResponse();
	}
}
